package com.opennews.openplatform.flink;

import com.opennews.openplatform.config.AppConfig;
import lombok.RequiredArgsConstructor;
import org.apache.flink.api.common.serialization.DeserializationSchema;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

@RequiredArgsConstructor
public class GenericDeserializationSchema<T> implements DeserializationSchema<T> {
    private static final ObjectMapper objectMapper;
    private final Class<T> clazz;

    static {
        objectMapper = new AppConfig().createObjectMapper();
    }

    @Override
    public T deserialize(byte[] message) throws IOException {
        return objectMapper.readValue(message, clazz);
    }

    @Override
    public boolean isEndOfStream(T nextElement) {
        return false;
    }

    @Override
    public TypeInformation<T> getProducedType() {
        return TypeInformation.of(clazz);
    }
}
